package code;

import java.util.concurrent.Semaphore;

/**
 * <p>
 *
 * </p>
 *
 * @author xiaovcloud
 * @since 2021/8/22 下午7:45
 */
public class SemaphoreTest {

	static int i =0;

	static Semaphore semaphore = new Semaphore(0);
	public static void main(String[] args){
		final Thread threadA = new Thread() {
			@Override
			public void run() {
				try {
					Thread.sleep(100);
					//自增
					i++;
				} catch (InterruptedException e) {
					e.printStackTrace();
				}
				semaphore.release(1);
			}

		};
		final Thread threadB = new Thread() {
			@Override
			public void run() {
				try {
					semaphore.acquire(2);
				} catch (InterruptedException e) {
					e.printStackTrace();
				}
				System.out.println("B =>:"+i);
				semaphore.release();
			}
		};
		final Thread threadC = new Thread() {

			@Override
			public void run() {
				try {
					semaphore.acquire();
				} catch (InterruptedException e) {
					e.printStackTrace();
				}
				System.out.println("C =>:"+i);
				semaphore.release(2);
			}
		};
		threadA.start();
		threadB.start();
		threadC.start();
	}
}
